package com.bootdo.wx.controller;

import com.bootdo.wx.domain.CmsContentDO;
import com.bootdo.wx.service.CmsContentService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分销设置
 * 
 * @author sys
 * @email 1992lcg@163.com
 * @date 2019-03-24 17:03:45
 */
 
@Controller
@RequestMapping("/wx/cmsContent")
public class CmsContentController {
	@Autowired
	private CmsContentService cmsContentService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsContent:cmsContent")
	String CmsContent(){
	    return "wx/cmsContent/cmsContent";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsContent:cmsContent")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CmsContentDO> cmsContentList = cmsContentService.list(query);
		int total = cmsContentService.count(query);
		PageUtils pageUtils = new PageUtils(cmsContentList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsContent:add")
	String add(){
	    return "wx/cmsContent/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsContent:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CmsContentDO cmsContent = cmsContentService.get(id);
		model.addAttribute("cmsContent", cmsContent);
	    return "wx/cmsContent/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsContent:add")
	public R save( CmsContentDO cmsContent){
		Map<String, Object> map = new HashMap<>();
		map.put("type", cmsContent.getType());
		List<CmsContentDO> list = cmsContentService.list(map);
		if(list.size()>0){
			return R.error("type类型不能重复");
		}
		if(cmsContentService.save(cmsContent)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsContent:edit")
	public R update( CmsContentDO cmsContent){
		Map<String, Object> map = new HashMap<>();
		map.put("type", cmsContent.getType());
		List<CmsContentDO> list = cmsContentService.list(map);
		if(list.size()>0){
			for (CmsContentDO cmsContentDO : list) {
				if(cmsContentDO.getId()!=cmsContent.getId()&&cmsContentDO.getType().equals(cmsContent.getType())){
					return R.error("类型不能重复");
				}
			}
		}
		cmsContentService.update(cmsContent);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsContent:remove")
	public R remove( Long id){
		if(cmsContentService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsContent:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		cmsContentService.batchRemove(ids);
		return R.ok();
	}

}
